package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/category")
@RestController
@Api(tags = "菜品分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询菜品分类
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询菜品分类")
    @GetMapping("/page")
    public Result<PageResult> pageQuery( CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询菜品分类,参数:{}",categoryPageQueryDTO);
       PageResult pageResult=categoryService.pageQuery(categoryPageQueryDTO);
       return Result.success(pageResult);
    }

    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "新增菜品分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增菜品分类,参数:{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 启用禁用菜品分类
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "启用禁用菜品分类")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("启用禁用菜品分类,参数:status={},id={}",status,id);
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 修改菜品分类信息
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "修改菜品分类信息")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改菜品分类信息,参数:{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 删除菜品分类
     * @param id
     * @return
     */
    @ApiOperation(value = "删除菜品分类")
    @DeleteMapping
    public Result deleteCategoryById(Long id){
        log.info("删除菜品分类,参数:id={}",id);
        categoryService.deteleteCategoryById(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @ApiOperation(value = "根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> listByType(Integer type){
        log.info("根据类型查询分类,参数:{}",type);
        List<Category> list=categoryService.listByType(type);
        return Result.success(list);
    }
}
